package org.flowers.unbeaten.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.flowers.unbeaten.base.entity.RoleMenu;
import org.flowers.unbeaten.base.vo.RolePermissionVo;
import org.flowers.unbeaten.mapper.RoleMenuMapper;
import org.flowers.unbeaten.service.IRoleMenuService;
import org.flowers.unbeaten.util.Id;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色菜单 服务实现类
 * </p>
 *
 * @author RyanWang
 * @since 2021-07-15
 */
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements IRoleMenuService {

    @Override
    public void add(RolePermissionVo vo) {
        List<RoleMenu> roleMenuList = new ArrayList<>();
        vo.getPermissions().forEach(permission -> {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setId(Id.generateId());
            roleMenu.setRoleId(vo.getRoleId());
            roleMenu.setMenuId(permission);
            roleMenuList.add(roleMenu);
        });
        // 删除之前绑定的数据
        this.remove(Wrappers.<RoleMenu>lambdaQuery().eq(RoleMenu::getRoleId, vo.getRoleId()));
        // 保存新数据
        this.saveBatch(roleMenuList);
    }
}
